#pragma once

#include <unordered_set>
#ifdef IO_MULTIPLEXING_MODE_EPOLL
#include <sys/epoll.h>
#endif
#include "SocketIO.hpp"
#include "SocketHelper.hpp"

#ifdef IO_MULTIPLEXING_MODE_EPOLL
#define MAX_EPOLL_FDS 16384
#endif


namespace cybertron {
	class SocketIOManager
	{
	public:
		SocketIOManager();
		~SocketIOManager();

		void add_io(SocketIO *s);
		void del_io(SocketIO *s);

		int perform_io(std::unordered_set<AcceptSocket *> *accepts, std::unordered_set<SocketIO *> *errors, int timeoutms = 100);

		void check_timeout();

		size_t io_size() { return _io_set_size; };
	private:
		std::unordered_set<SocketIO *> _io_set;
		time_t _last_check_timeout;
		size_t _io_set_size;
#ifdef IO_MULTIPLEXING_MODE_EPOLL
		int _epoll_fd;
		struct epoll_event* _epoll_events;
#else
		fd_set _rfds;
		fd_set _wfds;
		SOCKET _maxfd;
#endif
	};



}